package com.wyf.android.advance.rvvp

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.wyf.android.advance.R
import kotlinx.android.synthetic.main.activity_rvvp.*

class RVVPActivity : AppCompatActivity() {
    private var dataList = emptyList<RVVPViewData>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_rvvp)
        initData(0)
        initView()
    }

    private fun initData(idx:Int) {
        dataList = listOf(
            TopViewData("top1"),
            TopViewData("top2"),
            TopViewData("top3"),
            TopViewData("top4"),
            TopViewData("top5"),
            TopViewData("top6"),
            TabViewData(idx),
            BottomViewData(idx)
        )
    }

    private fun initView() {
        val rvvpAdapter = RVVPAdapter(supportFragmentManager) {

            initData(it)
            if (rv_rvvp.isComputingLayout || rv_rvvp.scrollState == RecyclerView.SCROLL_STATE_IDLE) {
                rv_rvvp.post {
                    (rv_rvvp.adapter as RVVPAdapter).submitList(dataList)
                }
            } else {
                (rv_rvvp.adapter as RVVPAdapter).submitList(dataList)
            }
        }
        rv_rvvp.apply {
            adapter = rvvpAdapter
        }

        rvvpAdapter.submitList(dataList)
    }

}